package com.cn.pet.controller;

import com.cn.pet.model.pojo.Brand;
import com.cn.pet.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class BrandController {

    @Autowired
    BrandService brandService;

    @RequestMapping(value = "/brands", method = RequestMethod.GET)
    public String listBrands(Model model) {
        model.addAttribute("brandList", brandService.findAllBrands());
        return "brand_list";
    }

    @ResponseBody
    @RequestMapping(value = "/brand", method = RequestMethod.GET)
    public List<Brand> listBrandAjax(){
        return brandService.findAllBrands();
    }

    @ResponseBody
    @RequestMapping(value = "/brand", method = RequestMethod.POST)
    public String addOrUpdateBrandAjax(@RequestBody Brand brand) {
        return brandService.addOrUpdateBrand(brand);
    }

    @ResponseBody
    @RequestMapping(value = "/brand/id/{id}", method = RequestMethod.POST)
    public String delBrandAjax(@PathVariable Integer id){
        return brandService.delBrand(id);
    }

}
